import { createRouter, createWebHistory } from 'vue-router'
import {useCounterStore} from "@/stores/counter.js";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
        path: '/',
        name: 'welcome',
        component: () => import('@/views/Welcome.vue'),
        children: [
          {
            path: '',
            name: 'welcome-login',
            component: () => import('@/components/welcome/LoginPage.vue')
          }, {
            path: 'register',
            name: 'welcome-register',
            component: () => import('@/components/welcome/RegisterPage.vue')
          }, {
            path: 'forgot',
            name: 'welcome-forgot',
            component: () => import('@/components/welcome/ForgotPassword.vue')
          }
        ]
      }, {
        path: '/index',
        name: 'index',
        component: () => import('@/views/IndexView.vue')
      }
  ]
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
  const store = useCounterStore();
  if (store.auth.user != null && to.name.startsWith('welcome')) {
    next('/index')
  } else if (store.auth.user == null && to.fullPath.startsWith('/index')) {
    next('/')
  } else if (to.matched.length === 0) {
    next('/index')
  } else {
    next()
  }
})
export default router
